<?php
/**
 * Created by PhpStorm.
 * User: zhao shou qiang
 * Date: 2017/12/12 0012
 * Time: 14:01
 */

namespace common\models;

class MyPager
{
    protected $total_rows;    // 总条数
    protected $list_rows;    // 每页显示条数
    protected $sub_pages;   // 预定显示页数
    protected $page_name;   //分页名称
    protected $now_page;    // 当前页

    //protected $first_row;    // 起始条数
    protected $total_pages;     // 总页数
    protected $page_array = array();//用来构造分页的数组

    /**
     * 作用：构造函数
     * 参数： array   一维
     */
    public function __construct($data = array('total_rows' => '', 'list_rows' => '', 'sub_pages' => '', 'page_name' => '', 'now_page' => ''))
    {
        $this->total_rows = $data['total_rows'];
        $this->list_rows = $data['list_rows'];
        $this->sub_pages = $data['sub_pages'];
        $this->page_name = !empty($data['page_name']) ? $data['page_name'] : 'page';

        $this->total_pages = ceil($this->total_rows / $this->list_rows);

        //当前页
        $this->now_page = !empty($data['now_page']) ? (int)$data['now_page'] : 1;
        $this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;
        if (!empty($this->total_pages) && $this->now_page > $this->total_pages) {
            $this->now_page = $this->total_pages;
        }

        // 起始条数
        //$this->first_row = $this->list_rows * ($this->now_page - 1);
    }


    /**
     *  作用：用来给建立分页的数组初始化的函数
     *  参数组 : void
     *  retrun : array 预定显示的页码
     */
    protected function initArray()
    {
        for ($i = 1; $i <= $this->sub_pages; $i++) {
            $this->page_array[$i] = $i;
        }
        return $this->page_array;
    }


    /**
     * 作用：构造显示的条数(寄｜偶) exp: 1 2 3 4 5 6 7 8 9 10  || 1 2 3 4 5
     * 预定显示页数为寄数时
     * 如果总页数小于等于预定显示页数加二，则全部显示   如： 1 2 3 4 5 6 7
     * 如果总页数大于等于预定显示页数加二
     * 如果当前页小于等于预定显示页数加一的一半并加一   如：  1 2 3 4 5 6 <a> ... </a>. 尾页 下一页
     * 如是当前页大于总页数减去预定显示页数加一的一半   如： 上一页 1<a> ... </a>   385 386 387 389
     * 其它     如： 上 一 页 1 <a> ... </a> 23 24 25 26 27 <a> ... </a> 389 下一页
     *
     * 预定显示页数为偶数时
     * 如果总页数小于等于预定显示页数加二，则全部显示    如： 1 2 3 4 5 6 7 8
     * 如果总页数大于等于预定显示页数加二
     * 如果当前页小于等于预定显示页数的一半并加二   如：上一页 1 2 3 4 5 6 7 <a> ... </a>尾页 下一页
     * 如是当前页大于总页数减去预定显示页的一半    如： 上一页 1 <a> ... </a>  385 386 387 389 下一页
     * 其它     如： 上 一 页 1 <a> ... </a> 23 24 25 26 27 28 <a> ... </a> 389 下一页
     * 参数：void
     * return: array 由当前显示的页数组成的数组
     */
    public function contsruct_num_Page()
    {
        $subPageCssStr = '<ul class="pagination pagination-sm">';
        $prev_page = $this->now_page - 1;
        $next_page = $this->now_page + 1;

        if ($this->sub_pages % 2 == 1) {  // 预定显示页数为寄数时
            // 如果总页数小于等于预定显示页数加二，则全部显示   如： 1 2 3 4 5 6 7
            if ($this->total_pages <= $this->sub_pages + 2) {
                $current_array = array();
                for ($i = 1; $i <= $this->total_pages; $i++) {
                    $current_array[$i] = $i;
                }

                $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'. $prev_page .'"><span aria-hidden="true">上一页</span></a></li>';

                foreach ($current_array as $val) {
                    if ($val == $this->now_page) {
                        $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';
                    } else {
                        $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                    }
                }

                $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';

                // 如果总页数大于等于预定显示页数加二
            } else {
                // 如果当前页小于等于预定显示页数加一的一半并加一   如：  1 2 3 4 5 6 <a> ... </a>. 尾页 下一页
                if ($this->now_page <= ($this->sub_pages + 1) / 2 + 1) {
                    $current_array = $this->initArray(); // 1 2 3 4 5
                    foreach ($current_array as $key => $val) {
                        $current_array[$key + 1] = $val + 1;  // 1 2 3 4 5 6
                    }

                    $subPageCssStr .= '<li class="disabled"><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'. $prev_page .'"><span aria-hidden="true">上一页</span></a></li>';

                    foreach ($current_array as $val) {
                        if ($val == $this->now_page) {
                            $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';

                        } else {
                            $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                        }
                    }
                    $subPageCssStr .= '<li><a class="omit">...</a></li><li><a href="javascript:void(0);" onclick="re_page('. $this->total_pages  .')" data-page="'. $this->total_pages .'">' . $this->total_pages . '</a></li>';
                    $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';

                    // 如是当前页大于总页数减去预定显示页数加一的一半   如： 上一页 1<a> ... </a>   385 386 387 389
                } else if ($this->now_page >= $this->total_pages - ($this->sub_pages + 1) / 2) {
                    $current_array = array();
                    for ($i = $this->total_pages; ($this->total_pages - $this->sub_pages) < $i; $i--) {
                        $current_array[$i - 1] = $i;
                    }
                    foreach ($current_array as $key => $val) {
                        $current_array[$key - 1] = $val - 1;
                    }
                    $current_array = array_reverse($current_array);  // 倒序

                    $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'. $prev_page .'"><span aria-hidden="true">上一页</span></a></li>
                                            <li><a href="javascript:void(0);">1</a></li><li><a class="omit">...</a></li>';

                    foreach ($current_array as $val) {
                        if ($val == $this->now_page) {
                            $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';
                        } else {
                            $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                        }
                    }
                    $subPageCssStr .= '<li class="disabled"><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';

                    // 其它     如： 上 一 页 1 <a> ... </a> 23 24 25 26 27 <a> ... </a> 389 下一页
                } else {
                    $current_array = array();
                    $a = $this->now_page - ($this->sub_pages - 1) / 2;
                    $b = $this->now_page + ($this->sub_pages - 1) / 2;
                    for ($i = $a; $i <= $b; $i++) {
                        $current_array[$i - 1] = $i;
                    }
                    $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'. $prev_page .'"><span aria-hidden="true">上一页</span></a></li>';
                    $subPageCssStr .= '<li><a href="javascript:void(0);">1</a></li><li><a class="omit">...</a></li>';

                    foreach ($current_array as $val) {
                        if ($val == $this->now_page) {
                            $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';
                        } else {
                            $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                        }
                    }

                    $subPageCssStr .= '<li><a class="omit">...</a></li><li><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->total_pages . '</a></li>';
                    $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';
                }
            }
            return $subPageCssStr;

        } else if ($this->sub_pages % 2 == 0) {  //	预定显示页数为偶数时
            // 如果总页数小于等于预定显示页数加二，则全部显示 如： 1 2 3 4 5 6 7 8
            if ($this->total_pages <= $this->sub_pages + 2) {
                $current_array = array();
                for ($i = 1; $i <= $this->total_pages; $i++) {
                    $current_array[$i] = $i;
                }

                $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'. $prev_page .'"><span aria-hidden="true">上一页</span></a></li>';

                foreach ($current_array as $val) {
                    if ($val == $this->now_page) {
                        $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';
                    } else {
                        $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                    }
                }

                $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';

                // 如果总页数大于等于预定显示页数加二
            } else {
                // 如果当前页小于等于预定显示页数的一半并加二   如：上一页 1 2 3 4 5 6 7 <a> ... </a>尾页 下一页
                if ($this->now_page <= ($this->sub_pages) / 2 + 2) {
                    $current_array = $this->initArray(); // 1 2 3 4 5
                    foreach ($current_array as $key => $val) {
                        $current_array[$key + 1] = $val + 1;  // 1 2 3 4 5 6
                    }

                    $subPageCssStr .= '<li class="disabled"><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'. $prev_page .'"><span aria-hidden="true">上一页</span></a></li>';

                    foreach ($current_array as $val) {
                        if ($val == $this->now_page) {
                            $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';
                        } else {
                            $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                        }
                    }

                    $subPageCssStr .= '<li><a class="omit">...</a></li><li><a href="javascript:void(0);" onclick="re_page('. $this->total_pages  .')" data-page="'. $this->total_pages .'">' . $this->total_pages . '</a></li>';
                    $subPageCssStr .= '<li ><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';

                //  如是当前页大于总页数减去预定显示页的一半    如： 上一页 1 <a> ... </a>  385 386 387 389 下一页
                } else if ($this->now_page >= $this->total_pages - ($this->sub_pages) / 2) {
                    $current_array = array();
                    for ($i = $this->total_pages; ($this->total_pages - $this->sub_pages) < $i; $i--) {
                        $current_array[$i - 1] = $i;
                    }
                    foreach ($current_array as $key => $val) {
                        $current_array[$key - 1] = $val - 1;
                    }
                    $current_array = array_reverse($current_array);  // 倒序

                    $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'. $prev_page .'"><span aria-hidden="true">上一页</span></a></li>
                                            <li><a href="javascript:void(0);">1</a></li><li><a class="omit">...</a></li>';

                    foreach ($current_array as $val) {
                        if ($val == $this->now_page) {
                            $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';
                        } else {
                            $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                        }
                    }

                    $subPageCssStr .= '<li class="disabled"><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';

                // 其它     如： 上 一 页 1 <a> ... </a> 23 24 25 26 27 28 <a> ... </a> 389 下一页
                } else {
                    $current_array = array();
                    $a = $this->now_page - $this->sub_pages / 2;
                    $b = $this->now_page + $this->sub_pages / 2 - 1;
                    for ($i = $a; $i <= $b; $i++) {
                        $current_array[$i - 1] = $i;
                    }

                    $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $prev_page  .')" aria-label="Previous" data-page="'.$prev_page .'"><span aria-hidden="true">上一页</span></a></li>';
                    $subPageCssStr .= '<li><a href="javascript:void(0);">1</a></li><li><a class="omit">...</a></li>';

                    foreach ($current_array as $val) {
                        if ($val == $this->now_page) {
                            $subPageCssStr .= '<li class="active"><a href="javascript:void(0);" onclick="re_page('. $this->now_page  .')" data-page="'. $this->now_page .'">' . $this->now_page . '</a></li>';
                        } else {
                            $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $val  .')" data-page="'. $val .'">' . $val . '</a></li>';
                        }
                    }

                    $subPageCssStr .= '<li><a class="omit">...</a></li><li><a href="javascript:void(0);" onclick="re_page('. $this->total_pages  .')" data-page="'. $this->total_pages .'">' . $this->total_pages . '</a></li>';
                    $subPageCssStr .= '<li><a href="javascript:void(0);" onclick="re_page('. $next_page  .')" aria-label="Next" data-page="'. $next_page .'"><span aria-hidden="true">下一页</span></a></li>';
                }
            }
            $subPageCssStr .= '</ul>';

            return $subPageCssStr;
        }
    }

}
